// 用户相关 小仓库
import { defineStore } from "pinia";
import { reqLogin } from "@/api/login"; // 登录接口
import { reqAllMenu } from "@/api/sys/menuApi" //全部菜单
import type { loginFormData, loginResData } from '@/api/login/type'
import type { UserState } from './types/type'
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN, SET_USERINFO, GET_USERINFO, REMOVE_USERINFO, SET_MENUINFO, GET_MENUINFO, REMOVE_MENUINFO } from "@/utils/token"; //本地存储方法
import { constantRoute,asnycRoute,anyRoute } from '@/router/routes' // 常量路由

//引入深拷贝方法
//@ts-expect-error
import cloneDeep from 'lodash/cloneDeep'
import router from '@/router'

//过滤当前用户需要展示的异步路由
function filterAsyncRoute(asnycRoute: any, routes: any) {
  return asnycRoute.filter((item: any) => {
    if (routes.includes(item.name)) {
      if (item.children && item.children.length > 0) {
        item.children = filterAsyncRoute(item.children, routes)
      }
      return true
    }
  })
}

let useUserStore = defineStore('User', {
  // 小仓库存储数据
  state: (): UserState => {
    return {
      token: GET_TOKEN(),
      menuRoutes: constantRoute,//仓库存储生成菜单需要的数组
      username: '', //用户登录账户名
    }
  },
  // 异步|逻辑
  actions: {
    // 用户登录方法
    async userLogin(data: loginFormData) {
      let result: loginResData = await reqLogin(data)
      if (result.code == 200) {
        this.token = (result.data.token as string)
        SET_TOKEN((result.data.token as string)) //存储token
        SET_USERINFO(result.data) // 存储用户信息
        
        let allMenuRes = await reqAllMenu()      
        SET_MENUINFO(allMenuRes.data.records) //存储全部菜单

        return 'ok'
      } else {
        return Promise.reject(new Error(result.msg))
      }
    },
    // 获取用户信息方法
    userInfo() {
      this.username = GET_USERINFO().username;
      //动态追加 当前用户需要展示的异步路由
      let allParentIds = GET_USERINFO().menuList.map((item:any) => item.parentId);
      let parentIds = [...new Set(allParentIds.filter((item:any) => item!==0))]; //当前用户所有一级菜单 去除首页
      let parentMenuIds:any = []; //一级菜单的MenuId集合
      GET_MENUINFO().forEach((item:any) => {
        if(parentIds.includes(item.id)){
          parentMenuIds.push(item.menuId)
        }
      })
      let menuRes = GET_USERINFO().menuList.map((item:any) => item.menuId); //当前用户菜单权限
      let temp = [...parentMenuIds,...menuRes]; //一级菜单+全部用户菜单
      const userAsyncRoute = filterAsyncRoute(cloneDeep(asnycRoute), [...new Set(temp)]);
      this.menuRoutes = [...constantRoute, ...userAsyncRoute, anyRoute];
      [...userAsyncRoute, anyRoute].forEach((route: any) => {
        router.addRoute(route)
      });
    },
    // ******  //
    // userInfo2(){
    //   this.username = GET_USERINFO().roleRealName; //roleRealName 登录用户名
    //   let menuRes = GET_USERINFO().menuList.map((item:any) => item.menuId); //当前用户菜单权限
    //   const userAsyncRoute = filterAsyncRoute(cloneDeep(asnycRoute), menuRes);
    //   this.menuRoutes = [...constantRoute, ...userAsyncRoute, anyRoute];
    //   [...userAsyncRoute, anyRoute].forEach((route: any) => {
    //     router.addRoute(route)
    //   });
    // },
    // 退出登录方法
    userLogout(){
      this.token = ''
      this.username = ''
      REMOVE_TOKEN()
      REMOVE_USERINFO()
      REMOVE_MENUINFO()
    },
  },
  getters: {

  }
})
export default useUserStore